from wordcloud import WordCloud
import matplotlib.pyplot as plt
from PIL import Image
import numpy as np

with open('关于实施乡村振兴战略的意见.txt', 'r', encoding='utf-8') as file:
    text = file.read()

mk = np.array(Image.open('alice_color.png'))

image_colors = np.array(Image.open('alice_color.png'))

wc = WordCloud(background_color="white", mask=mk,)
wc.generate(text)
ﬁg, axes = plt.subplots(1, 3)
axes[0].imshow(wc)
axes[1].imshow(wc.recolor(color_func=image_colors), interpolation="bilinear")
axes[2].imshow(mk, cmap=plt.cm.gray)
for ax in axes:
    ax.set_axis_oﬀ()
plt.show()
wc_color = wc.recolor(color_func=image_colors)
wc_color.to_ﬁle('output10-alice.png')